package com.ztesoft.web.checkup;

import java.util.concurrent.Callable;

import com.ztesoft.core.spring.context.SpringApplicationContextHolder;
import com.ztesoft.framework.log.ZTEsoftLogManager;
import com.ztesoft.framework.util.DateUtils;
import com.ztesoft.web.baseconfig.db.po.DmPlanPO;
import com.ztesoft.web.baseconfig.db.po.DmStrategyPO;
import com.ztesoft.web.checkup.service.CheckUpFileTaskService;
import com.ztesoft.web.common.DMSConstant;
import com.ztesoft.web.common.customdb.dao.DmTaskStageCustomDao;
import com.ztesoft.web.common.customdb.dao.DmTaskStageDefineCustomDao;
import com.ztesoft.web.common.customdb.dao.dto.DmTaskStageDto;
import com.ztesoft.web.filecommon.db.po.DmSrcFilePO;
import com.ztesoft.web.taskgenerator.db.dao.DmTaskDao;
import com.ztesoft.web.taskgenerator.db.po.DmTaskPO;
import com.ztesoft.web.taskgenerator.db.po.DmTaskStageDefinePO;

/**
 * 体检处理
 * 
 */
public class CheckUpFileTask implements Callable<DmTaskStageDto> {
	
	private static final ZTEsoftLogManager LOGGER = ZTEsoftLogManager.getLogger(CheckUpFileTask.class);

	private DmTaskStageDto taskStagePO;//
	
	private DmSrcFilePO dmSrcFilePO;//
	
	private DmPlanPO planPO;
	private int taskId;
	
	private DmStrategyPO strategyPO;

	private DmTaskStageCustomDao taskStageCustomDao;

	private DmTaskStageDefineCustomDao taskStageDefineDao;
	
	private DmTaskDao taskDao;
	
	private CheckUpFileTaskService checkUpFileTaskService;
	
	public CheckUpFileTask(DmSrcFilePO dmSrcFilePO, DmTaskStageDto taskStagePO, int taskId, 
			DmStrategyPO strategyPO, DmPlanPO planPO) {
		this.planPO = planPO;
		this.taskStagePO = taskStagePO;
		this.dmSrcFilePO = dmSrcFilePO;
		this.strategyPO = strategyPO;
		this.taskId = taskId;
		taskDao = SpringApplicationContextHolder.getBean(DmTaskDao.class);
		taskStageCustomDao = (DmTaskStageCustomDao) SpringApplicationContextHolder.getBean("TaskStageCustomDao");
		taskStageDefineDao = (DmTaskStageDefineCustomDao) SpringApplicationContextHolder.getBean("TaskStageDefineCustomDao");
		checkUpFileTaskService = (CheckUpFileTaskService) SpringApplicationContextHolder.getBean("CheckUpFileTaskServiceImpl");
	}
	

	@Override
	public DmTaskStageDto call() {
		//
		taskStagePO.setTaskStartTime((taskStagePO.getTaskStartTime()!=null)?null:DateUtils.getDBCurrentTime());
		taskStagePO.setState(DMSConstant.Status.PROCESSING.getState());
		taskStagePO.setTaskEndTime(null);//避免带入条件
		taskStagePO.setCheckDataSize(null);
		taskStagePO.setErrorDesc(null);
		taskStageCustomDao.updateTaskStageState(taskStagePO);//
		return addTask();
	}
	
	private DmTaskStageDto addTask() {
		String state = null;
		DmTaskStageDefinePO taskStageDefinePO = taskStageDefineDao.selectTaskStage(planPO.getOperType(), 
				DMSConstant.STAGE_CHECKUP, null);
		if ("F".equals(taskStageDefinePO.getIsLastStage())) {			
			taskStageDefinePO = taskStageDefineDao.selectTaskStage(planPO.getOperType(), null, 
					taskStageDefinePO.getPriority()+1);
		}
		if (null == taskStageDefinePO.getStage() || DMSConstant.STAGE_CHECKUP == taskStageDefinePO.getStage()) {//
			state = DMSConstant.Status.SUCCESS.getState();//已完成
			DmTaskPO taskPO = new DmTaskPO();
			taskPO.setDmTaskId(taskId);
			taskPO.setNeedSubtask(DMSConstant.NEED_SUBTASK_F);
			taskPO.setStateDate(DateUtils.getDBCurrentTime());
			taskDao.updateByPrimaryKey(taskPO);
			taskStagePO.setCheckCount(taskStagePO.getTotalRecordCount());
			taskStagePO.setCheckDataSize(taskStagePO.getTotalDataSize());
			taskStagePO.setTaskStartTime(DateUtils.getDBCurrentTime());
			taskStageCustomDao.updateTaskStageCheckCount(taskStagePO);
			taskStagePO.setCheckState(DMSConstant.SUCCESS);
			return taskStagePO;
		} 
		//执行体检
		try {
			checkUpFileTaskService.addCheckUp(state, strategyPO, taskId, dmSrcFilePO, taskStagePO, planPO);
		} catch (Exception e) {
			taskStagePO.setCheckState(DMSConstant.FAIL);
			LOGGER.error("checkup error:",e);
		}
		return taskStagePO;
	}


}
